%% Aim
% Perform advanced statistical analyses on aDDM outputs

%% Setup
clc;
clear;
root_path = pwd; 
addpath(genpath(root_path));

nrole=2;
nblock=2;
ntrial=100;
nprice=10;
nlottery=10;

nchoice=2;

role_label={'Buyer','Seller','Contrast'};
role_color_dark={[235/255 112/255 99/255],[106/255 142/255 208/255]}; 
role_color_median={[238/255 137/255 126/255],[144/255 171/255 220/255],[0.75 0.75 0.75]}; 
role_color_light={[243/255 171/255 163/255],[165/255 187/255 227/255]};

attribute_label={'Price','Lottery'};
attribute_color={[91/255,155/255,213/255],[112/255,173/255,71/255],[166/255,166/255,166/255]}; % Price vs. Lottery

bias_label={'Val','Res','Val vs. Res'};
bias_color_median={[195/255 205/255 147/255],[192/255 173/255 203/255]}; 

choice_label={'Accept','Reject'};
choice_color={[192/256 0/256 0/256],[0/256 32/256 102/256]};
choice_color_light={[255/256 109/256 109/256],[133/256 171/256 255/256]};

%% Load data
sub_data=readtable([root_path,'\data\subject\subject.csv']);
sub_beh_data=readtable([root_path,'\data\subject\subject_behavior.csv']);
sub_ddm_data=readtable([root_path,'\data\subject\subject_ddm.csv']);
beh_data=readtable([root_path,'\data\behavior\behavior.csv']);
gaze_trial_data=readtable([root_path,'\data\gaze\gaze_trial.csv']);
eye_trial_data=readtable([root_path,'\data\eye\eye_trial.csv']);
nsub=height(sub_data);

%% Concatenated data
sub_data = [sub_data, sub_beh_data(:,4:end), sub_ddm_data(:,4:end)];
trial_data = [beh_data, eye_trial_data(:,9:end), gaze_trial_data(:,9:end)];

%%







%% aDDM4a: a reduced form of the conventional aDDM 

raw_data=readtable([root_path,'\model\hddm\aDDM4a\aDDM4a_trace.csv']);

addm_trace_data=table;
addm_trace_data.t=raw_data.t;
addm_trace_data.aBuy=raw_data.a_Intercept;
addm_trace_data.aDelta=raw_data.a_C_Role__T_2_;
addm_trace_data.aSell=raw_data.a_Intercept+raw_data.a_C_Role__T_2_;
addm_trace_data.vInterceptBuy=raw_data.v_Intercept;
addm_trace_data.vInterceptDelta=raw_data.v_C_Role__T_2_;
addm_trace_data.vInterceptSell=raw_data.v_Intercept + raw_data.v_C_Role__T_2_;
addm_trace_data.vPrice=raw_data.v_Price;
addm_trace_data.vLotteryBuyGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__1_;
addm_trace_data.vLotteryBuyGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__1_;
addm_trace_data.vLotterySellGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__2_;
addm_trace_data.vLotterySellGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__2_;
addm_trace_data.wLotteryBuyGazeOnPrice   =addm_trace_data.vLotteryBuyGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotteryBuyGazeOnLottery =addm_trace_data.vLotteryBuyGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnPrice  =addm_trace_data.vLotterySellGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnLottery=addm_trace_data.vLotterySellGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnPrice   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnPriceBuyOnLottery   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.ValBiasSellOnLotteryBuyOnPrice   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnLotteryBuyOnLottery   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.zBuy=exp(raw_data.z_Intercept_trans)./(1+exp(raw_data.z_Intercept_trans));
addm_trace_data.zDelta=raw_data.z_C_Role__T_2_;
addm_trace_data.zSell=addm_trace_data.zBuy+addm_trace_data.zDelta;
addm_trace_data.res=addm_trace_data.zSell - addm_trace_data.zBuy;
addm_trace_data.vInterceptBias=addm_trace_data.vInterceptSell - addm_trace_data.vInterceptBuy;
addm_trace_data.aBias=addm_trace_data.aSell - addm_trace_data.aBuy;
addm_trace_data.ratioBuy=(addm_trace_data.wLotteryBuyGazeOnPrice./addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ratioSell=(addm_trace_data.wLotterySellGazeOnPrice./addm_trace_data.wLotterySellGazeOnLottery);
addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ValBiasSellOnLotteryBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnLotteryBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery);
writetable(addm_trace_data,[root_path,'\model\hddm\aDDM4a\aDDM4a_trace_group.csv']);

% mean and 95% credible intervals
A=mean(table2array(addm_trace_data));
B=prctile(table2array(addm_trace_data),2.5);
C=prctile(table2array(addm_trace_data),97.5);
rowNames = {'mean','2.5p','97.5p'};
colNames = {'t','aBuy','aDelta','aSell','vInterceptBuy','vInterceptDelta','vInterceptSell','vPrice',...
            'vLotteryBuyGazeOnPrice','vLotteryBuyGazeOnLottery','vLotterySellGazeOnPrice','vLotterySellGazeOnLottery',...
            'wLotteryBuyGazeOnPrice','wLotteryBuyGazeOnLottery','wLotterySellGazeOnPrice','wLotterySellGazeOnLottery',...
            'ValBiasSellOnPriceBuyOnPrice','ValBiasSellOnPriceBuyOnLottery','ValBiasSellOnLotteryBuyOnPrice','ValBiasSellOnLotteryBuyOnLottery',...
            'zBuy','zDelta','zSell',...
            'ResBias','vInterBias','aBias',...
            'ratioBuy','ratioSell',...
            'ValBiasSellOnPriceBuyOnPriceLog','ValBiasSellOnPriceBuyOnLotteryLog','ValBiasSellOnLotteryBuyOnPriceLog','ValBiasSellOnLotteryBuyOnLotteryLog'};
addm.group.post.describe = array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames);
disp(addm.group.post.describe);

% likelihood of difference between buyer and seller
addm.group.likelihood.ratioBuy=mean(addm_trace_data.ratioBuy < 1);
addm.group.likelihood.ratioSell=mean(addm_trace_data.ratioSell < 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog > 0);
addm.group.likelihood.ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog > 0);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 0);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > 0);
addm.group.likelihood.res=mean(addm_trace_data.zSell > addm_trace_data.zBuy);
addm.group.likelihood.a=mean(addm_trace_data.aSell > addm_trace_data.aBuy);
addm.group.likelihood.vIntercept=mean(addm_trace_data.vInterceptDelta > 0);
disp(addm.group.likelihood);

% largest
disp(mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > addm_trace_data.ValBiasSellOnPriceBuyOnPrice));
disp(mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > addm_trace_data.ValBiasSellOnLotteryBuyOnLottery));
disp(mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > addm_trace_data.ValBiasSellOnPriceBuyOnLottery));   

% smallest
disp(mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery < addm_trace_data.ValBiasSellOnPriceBuyOnPrice));
disp(mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery < addm_trace_data.ValBiasSellOnLotteryBuyOnLottery));
disp(mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery < addm_trace_data.ValBiasSellOnLotteryBuyOnPrice)); 
% no larger than 1
disp(mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery > 1));

% both attend to lottery
disp(mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 1));
% both attend to price
disp(mean(addm_trace_data.ValBiasSellOnPriceBuyOnPrice > 1));

%% Plot: weighting of lottery
figure('Renderer', 'painters', 'Position', [10 10 380 350]);
ax=subplot(1,1,1);
temp=addm.group.post.describe;
ab=bar([1 2 3 4],[temp.wLotteryBuyGazeOnPrice(1) temp.wLotteryBuyGazeOnLottery(1) temp.wLotterySellGazeOnPrice(1) temp.wLotterySellGazeOnLottery(1)],'EdgeColor','none','FaceColor',[0.75 0.75 0.75],'barwidth',0.6);
ab.FaceColor='flat';
ab.CData(2,:) = role_color_dark{1};
ab.CData(1,:) = role_color_light{1};
ab.CData(4,:) = role_color_dark{2};
ab.CData(3,:) = role_color_light{2};
hold on
er=errorbar([1 2 3 4],[temp.wLotteryBuyGazeOnPrice(1) temp.wLotteryBuyGazeOnLottery(1) temp.wLotterySellGazeOnPrice(1) temp.wLotterySellGazeOnLottery(1)],...
                            [temp.wLotteryBuyGazeOnPrice(1)-temp.wLotteryBuyGazeOnPrice(2) temp.wLotteryBuyGazeOnLottery(1)-temp.wLotteryBuyGazeOnLottery(2) temp.wLotterySellGazeOnPrice(1)-temp.wLotterySellGazeOnPrice(2) temp.wLotterySellGazeOnLottery(1)-temp.wLotterySellGazeOnLottery(2)],...
                            [temp.wLotteryBuyGazeOnPrice(3)-temp.wLotteryBuyGazeOnPrice(1) temp.wLotteryBuyGazeOnLottery(3)-temp.wLotteryBuyGazeOnLottery(1) temp.wLotterySellGazeOnPrice(3)-temp.wLotterySellGazeOnPrice(1) temp.wLotterySellGazeOnLottery(3)-temp.wLotterySellGazeOnLottery(1)],...
                            'Color',[0 0 0]);
er.LineStyle = 'none';                           
% Edit figure
set(gca,'TickDir','out');
xticklabels({'BuyerP','BuyerL','SellerP','SellerL'});
ylabel('wLottery');
% yticks(1:0.5:2.5);
ylim([0 1]);
hold off
print(1, '-dtiff', [root_path,'\figure\model\addm\wLottery_addm.tif'], '-r200');
close;

%% Plot: valuation bias
figure('Renderer', 'painters', 'Position', [10 10 380 350]);
ax=subplot(1,1,1);
temp=addm.group.post.describe;
ab=bar([1 2 3 4],[temp.ValBiasSellOnPriceBuyOnLotteryLog(1) temp.ValBiasSellOnPriceBuyOnPriceLog(1) temp.ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.ValBiasSellOnLotteryBuyOnPriceLog(1)],'EdgeColor','none','FaceColor',bias_color_median{1},'barwidth',0.6);
ab.FaceColor='flat';
% ab.CData(1,:) = attribute_color{1};
% ab.CData(2,:) = attribute_color{2};
% ab.CData(3,:) = attribute_color{1};
% ab.CData(4,:) = attribute_color{2};
hold on
er=errorbar([1 2 3 4],[temp.ValBiasSellOnPriceBuyOnLotteryLog(1) temp.ValBiasSellOnPriceBuyOnPriceLog(1) temp.ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.ValBiasSellOnLotteryBuyOnPriceLog(1)],...
                            [temp.ValBiasSellOnPriceBuyOnLotteryLog(1)-temp.ValBiasSellOnPriceBuyOnLotteryLog(2) temp.ValBiasSellOnPriceBuyOnPriceLog(1)-temp.ValBiasSellOnPriceBuyOnPriceLog(2) temp.ValBiasSellOnLotteryBuyOnLotteryLog(1)-temp.ValBiasSellOnLotteryBuyOnLotteryLog(2) temp.ValBiasSellOnLotteryBuyOnPriceLog(1)-temp.ValBiasSellOnLotteryBuyOnPriceLog(2)],...
                            [temp.ValBiasSellOnPriceBuyOnLotteryLog(3)-temp.ValBiasSellOnPriceBuyOnLotteryLog(1) temp.ValBiasSellOnPriceBuyOnPriceLog(3)-temp.ValBiasSellOnPriceBuyOnPriceLog(1) temp.ValBiasSellOnLotteryBuyOnLotteryLog(3)-temp.ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.ValBiasSellOnLotteryBuyOnPriceLog(3)-temp.ValBiasSellOnLotteryBuyOnPriceLog(1)],...
                            'Color',[0 0 0]);
er.LineStyle = 'none';                           
% Edit figure
set(gca,'TickDir','out');
% xticklabels({'BuyerOnPrice','BuyerOnLottery','SellerOnPrice','SellerOnLottery'});
ylabel('Valuation Bias');
% yticks(1:0.5:2.5);
ylim([-0.4 1]);
% yticks(0:1:3);
% line(xlim,[1 1],'Color',[0 0 0],'LineStyle','--');
hold off
print(1, '-dtiff', [root_path,'\figure\model\addm\val_addm_log.tif'], '-r200');
close;
      
%% 










%% aDDM2a: a reduced form of the conventional aDDM 

raw_data=readtable([root_path,'\model\hddm\aDDM2a\aDDM2a_trace.csv']);

addm_trace_data=table;
addm_trace_data.t=raw_data.t;
addm_trace_data.aBuy=raw_data.a_Intercept;
addm_trace_data.aDelta=raw_data.a_C_Role__T_2_;
addm_trace_data.aSell=raw_data.a_Intercept+raw_data.a_C_Role__T_2_;
addm_trace_data.vInterceptBuy=raw_data.v_Intercept;
addm_trace_data.vInterceptDelta=raw_data.v_C_Role__T_2_;
addm_trace_data.vInterceptSell=raw_data.v_Intercept + raw_data.v_C_Role__T_2_;
addm_trace_data.vPrice=raw_data.v_Price;
addm_trace_data.vLotteryBuyGazeOnPrice=raw_data.v_Lottery_C_Role__1_;
addm_trace_data.vLotteryBuyGazeOnLottery=raw_data.v_Lottery_C_Role__1_;
addm_trace_data.vLotterySellGazeOnPrice=raw_data.v_Lottery_C_Role__2_;
addm_trace_data.vLotterySellGazeOnLottery=raw_data.v_Lottery_C_Role__2_;
addm_trace_data.wLotteryBuyGazeOnPrice   =addm_trace_data.vLotteryBuyGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotteryBuyGazeOnLottery =addm_trace_data.vLotteryBuyGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnPrice  =addm_trace_data.vLotterySellGazeOnPrice./(-addm_trace_data.vPrice);
addm_trace_data.wLotterySellGazeOnLottery=addm_trace_data.vLotterySellGazeOnLottery./(-addm_trace_data.vPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnPrice   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnPriceBuyOnLottery   = addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.ValBiasSellOnLotteryBuyOnPrice   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice;
addm_trace_data.ValBiasSellOnLotteryBuyOnLottery   = addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery;
addm_trace_data.zBuy=exp(raw_data.z_Intercept_trans)./(1+exp(raw_data.z_Intercept_trans));
addm_trace_data.zDelta=raw_data.z_C_Role__T_2_;
addm_trace_data.zSell=addm_trace_data.zBuy+addm_trace_data.zDelta;
addm_trace_data.res=addm_trace_data.zSell - addm_trace_data.zBuy;
addm_trace_data.vInterceptBias=addm_trace_data.vInterceptSell - addm_trace_data.vInterceptBuy;
addm_trace_data.aBias=addm_trace_data.aSell - addm_trace_data.aBuy;
addm_trace_data.ratioBuy=(addm_trace_data.wLotteryBuyGazeOnPrice./addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ratioSell=(addm_trace_data.wLotterySellGazeOnPrice./addm_trace_data.wLotterySellGazeOnLottery);
addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ValBiasSellOnLotteryBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.ValBiasSellOnLotteryBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery);
writetable(addm_trace_data,[root_path,'\model\hddm\aDDM2a\aDDM2a_trace_group.csv']);

% mean and 95% credible intervals
A=mean(table2array(addm_trace_data));
B=prctile(table2array(addm_trace_data),2.5);
C=prctile(table2array(addm_trace_data),97.5);
rowNames = {'mean','2.5p','97.5p'};
colNames = {'t','aBuy','aDelta','aSell','vInterceptBuy','vInterceptDelta','vInterceptSell','vPrice',...
            'vLotteryBuyGazeOnPrice','vLotteryBuyGazeOnLottery','vLotterySellGazeOnPrice','vLotterySellGazeOnLottery',...
            'wLotteryBuyGazeOnPrice','wLotteryBuyGazeOnLottery','wLotterySellGazeOnPrice','wLotterySellGazeOnLottery',...
            'ValBiasSellOnPriceBuyOnPrice','ValBiasSellOnPriceBuyOnLottery','ValBiasSellOnLotteryBuyOnPrice','ValBiasSellOnLotteryBuyOnLottery',...
            'zBuy','zDelta','zSell',...
            'ResBias','vInterBias','aBias',...
            'ratioBuy','ratioSell',...
            'ValBiasSellOnPriceBuyOnPriceLog','ValBiasSellOnPriceBuyOnLotteryLog','ValBiasSellOnLotteryBuyOnPriceLog','ValBiasSellOnLotteryBuyOnLotteryLog'};
addm.group.post.describe = array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames);
disp(addm.group.post.describe);

% likelihood of difference between buyer and seller
addm.group.likelihood.ratioBuy=mean(addm_trace_data.ratioBuy < 1);
addm.group.likelihood.ratioSell=mean(addm_trace_data.ratioSell < 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.ValBiasSellOnPriceBuyOnPrice > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > 1);
addm.group.likelihood.res=mean(addm_trace_data.zSell > addm_trace_data.zBuy);
addm.group.likelihood.a=mean(addm_trace_data.aSell > addm_trace_data.aBuy);
addm.group.likelihood.vIntercept=mean(addm_trace_data.vInterceptDelta > 0);
disp(addm.group.likelihood);

%%










%% aDDM6a: Group posteriors identified from aHDDM traces

raw_data=readtable([root_path,'\model\hddm\aDDM6a\aDDM6a_trace.csv']);

addm_trace_data=table;
addm_trace_data.t=raw_data.t;
addm_trace_data.aBuy=raw_data.a_Intercept;
addm_trace_data.aDelta=raw_data.a_C_Role__T_2_;
addm_trace_data.aSell=raw_data.a_Intercept+raw_data.a_C_Role__T_2_;
addm_trace_data.vInterceptBuy=raw_data.v_Intercept;
addm_trace_data.vInterceptDelta=raw_data.v_C_Role__T_2_;
addm_trace_data.vInterceptSell=raw_data.v_Intercept + raw_data.v_C_Role__T_2_;
addm_trace_data.vPriceGazeOnPrice=raw_data.v_PriceGazeDurPriceRatio;
addm_trace_data.vPriceGazeOnLottery=raw_data.v_PriceGazeDurLotteryRatio;
addm_trace_data.vLotteryBuyGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__1_;
addm_trace_data.vLotteryBuyGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__1_;
addm_trace_data.vLotterySellGazeOnPrice=raw_data.v_LotteryGazeDurPriceRatio_C_Role__2_;
addm_trace_data.vLotterySellGazeOnLottery=raw_data.v_LotteryGazeDurLotteryRatio_C_Role__2_;
addm_trace_data.theta=addm_trace_data.vPriceGazeOnLottery./addm_trace_data.vPriceGazeOnPrice; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
addm_trace_data.wLotteryBuyGazeOnPrice   =addm_trace_data.vLotteryBuyGazeOnPrice./(-addm_trace_data.vPriceGazeOnLottery);
addm_trace_data.wLotteryBuyGazeOnLottery =addm_trace_data.vLotteryBuyGazeOnLottery./(-addm_trace_data.vPriceGazeOnPrice);
addm_trace_data.wLotterySellGazeOnPrice   =addm_trace_data.vLotterySellGazeOnPrice./(-addm_trace_data.vPriceGazeOnLottery);
addm_trace_data.wLotterySellGazeOnLottery =addm_trace_data.vLotterySellGazeOnLottery./(-addm_trace_data.vPriceGazeOnPrice);
addm_trace_data.ValBiasSellOnPriceBuyOnPrice   = (addm_trace_data.wLotterySellGazeOnPrice.*addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnPrice.*addm_trace_data.theta);
addm_trace_data.ValBiasSellOnPriceBuyOnLottery   = (addm_trace_data.wLotterySellGazeOnPrice.*addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnLottery./addm_trace_data.theta);
addm_trace_data.ValBiasSellOnLotteryBuyOnPrice   = (addm_trace_data.wLotterySellGazeOnLottery./addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnPrice.*addm_trace_data.theta);
addm_trace_data.ValBiasSellOnLotteryBuyOnLottery   = (addm_trace_data.wLotterySellGazeOnLottery./addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnLottery./addm_trace_data.theta);
addm_trace_data.zBuy=exp(raw_data.z_Intercept_trans)./(1+exp(raw_data.z_Intercept_trans));
addm_trace_data.zDelta=raw_data.z_C_Role__T_2_;
addm_trace_data.zSell=addm_trace_data.zBuy+addm_trace_data.zDelta;
addm_trace_data.res=addm_trace_data.zSell - addm_trace_data.zBuy;
addm_trace_data.vInterceptBias=addm_trace_data.vInterceptSell - addm_trace_data.vInterceptBuy;
addm_trace_data.aBias=addm_trace_data.aSell - addm_trace_data.aBuy;
addm_trace_data.ratioBuy=(addm_trace_data.wLotteryBuyGazeOnPrice./addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.ratioSell=(addm_trace_data.wLotterySellGazeOnPrice./addm_trace_data.wLotterySellGazeOnLottery);
addm_trace_data.raw_ValBiasSellOnPriceBuyOnPrice   = (addm_trace_data.wLotterySellGazeOnPrice) ./ (addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.raw_ValBiasSellOnPriceBuyOnLottery   = (addm_trace_data.wLotterySellGazeOnPrice) ./ (addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.raw_ValBiasSellOnLotteryBuyOnPrice   = (addm_trace_data.wLotterySellGazeOnLottery) ./ (addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.raw_ValBiasSellOnLotteryBuyOnLottery   = (addm_trace_data.wLotterySellGazeOnLottery) ./ (addm_trace_data.wLotteryBuyGazeOnLottery);

addm_trace_data.ValBiasSellOnPriceBuyOnPriceLog   =log( (addm_trace_data.wLotterySellGazeOnPrice.*addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnPrice.*addm_trace_data.theta));
addm_trace_data.ValBiasSellOnPriceBuyOnLotteryLog   =log( (addm_trace_data.wLotterySellGazeOnPrice.*addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnLottery./addm_trace_data.theta));
addm_trace_data.ValBiasSellOnLotteryBuyOnPriceLog   =log( (addm_trace_data.wLotterySellGazeOnLottery./addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnPrice.*addm_trace_data.theta));
addm_trace_data.ValBiasSellOnLotteryBuyOnLotteryLog   =log( (addm_trace_data.wLotterySellGazeOnLottery./addm_trace_data.theta) ./ (addm_trace_data.wLotteryBuyGazeOnLottery./addm_trace_data.theta));
addm_trace_data.raw_ValBiasSellOnPriceBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.raw_ValBiasSellOnPriceBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnPrice ./ addm_trace_data.wLotteryBuyGazeOnLottery);
addm_trace_data.raw_ValBiasSellOnLotteryBuyOnPriceLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnPrice);
addm_trace_data.raw_ValBiasSellOnLotteryBuyOnLotteryLog   = log(addm_trace_data.wLotterySellGazeOnLottery ./ addm_trace_data.wLotteryBuyGazeOnLottery);

writetable(addm_trace_data,[root_path,'\model\hddm\aDDM6a\aDDM6a_trace_group.csv']);

% mean and 95% credible intervals
A=mean(table2array(addm_trace_data));
B=prctile(table2array(addm_trace_data),2.5);
C=prctile(table2array(addm_trace_data),97.5);
rowNames = {'mean','2.5p','97.5p'};
colNames = {'t','aBuy','aDelta','aSell',...
            'vInterceptBuy','vInterceptDelta','vInterceptSell',...
            'vPriceGazeOnPrice','vPriceGazeOnLottery',...
            'vLotteryBuyGazeOnPrice','vLotteryBuyGazeOnLottery','vLotterySellGazeOnPrice','vLotterySellGazeOnLottery',...
            'theta',...
            'wLotteryBuyGazeOnPrice','wLotteryBuyGazeOnLottery','wLotterySellGazeOnPrice','wLotterySellGazeOnLottery',...
            'ValBiasSellOnPriceBuyOnPrice','ValBiasSellOnPriceBuyOnLottery','ValBiasSellOnLotteryBuyOnPrice','ValBiasSellOnLotteryBuyOnLottery',...
            'zBuy','zDelta','zSell',...
            'ResBias','vInterBias','aBias',...            
            'ratioBuy','ratioSell'...
            'raw_ValBiasSellOnPriceBuyOnPrice','raw_ValBiasSellOnPriceBuyOnLottery','raw_ValBiasSellOnLotteryBuyOnPrice','raw_ValBiasSellOnLotteryBuyOnLottery',...  
            'ValBiasSellOnPriceBuyOnPriceLog','ValBiasSellOnPriceBuyOnLotteryLog','ValBiasSellOnLotteryBuyOnPriceLog','ValBiasSellOnLotteryBuyOnLotteryLog',...
            'raw_ValBiasSellOnPriceBuyOnPriceLog','raw_ValBiasSellOnPriceBuyOnLotteryLog','raw_ValBiasSellOnLotteryBuyOnPriceLog','raw_ValBiasSellOnLotteryBuyOnLotteryLog',...              
            };
addm.group.post.describe = array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames);
disp(addm.group.post.describe);

% likelihood of difference between buyer and seller
addm.group.likelihood.ratioBuy=mean(addm_trace_data.ratioBuy < 1);
addm.group.likelihood.ratioSell=mean(addm_trace_data.ratioSell < 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.ValBiasSellOnPriceBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.ValBiasSellOnPriceBuyOnPrice > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnLottery > 1);
addm.group.likelihood.ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.ValBiasSellOnLotteryBuyOnPrice > 1);
addm.group.likelihood.res=mean(addm_trace_data.zSell > addm_trace_data.zBuy);
addm.group.likelihood.a=mean(addm_trace_data.aSell > addm_trace_data.aBuy);
addm.group.likelihood.vIntercept=mean(addm_trace_data.vInterceptDelta > 0);
addm.group.likelihood.raw_ValBiasSellOnPriceBuyOnLottery=mean(addm_trace_data.raw_ValBiasSellOnPriceBuyOnLottery > 1);
addm.group.likelihood.raw_ValBiasSellOnPriceBuyOnPrice=mean(addm_trace_data.raw_ValBiasSellOnPriceBuyOnPrice > 1);
addm.group.likelihood.raw_ValBiasSellOnLotteryBuyOnLottery=mean(addm_trace_data.raw_ValBiasSellOnLotteryBuyOnLottery > 1);
addm.group.likelihood.raw_ValBiasSellOnLotteryBuyOnPrice=mean(addm_trace_data.raw_ValBiasSellOnLotteryBuyOnPrice > 1);
disp(addm.group.likelihood);

%% Plot: weighting of lottery
figure('Renderer', 'painters', 'Position', [10 10 380 350]);
ax=subplot(1,1,1);
temp=addm.group.post.describe;
ab=bar([1 2 3 4],[temp.wLotteryBuyGazeOnPrice(1) temp.wLotteryBuyGazeOnLottery(1) temp.wLotterySellGazeOnPrice(1) temp.wLotterySellGazeOnLottery(1)],'EdgeColor','none','FaceColor',[0.75 0.75 0.75],'barwidth',0.6);
ab.FaceColor='flat';
ab.CData(2,:) = role_color_dark{1};
ab.CData(1,:) = role_color_light{1};
ab.CData(4,:) = role_color_dark{2};
ab.CData(3,:) = role_color_light{2};
hold on
er=errorbar([1 2 3 4],[temp.wLotteryBuyGazeOnPrice(1) temp.wLotteryBuyGazeOnLottery(1) temp.wLotterySellGazeOnPrice(1) temp.wLotterySellGazeOnLottery(1)],...
                            [temp.wLotteryBuyGazeOnPrice(1)-temp.wLotteryBuyGazeOnPrice(2) temp.wLotteryBuyGazeOnLottery(1)-temp.wLotteryBuyGazeOnLottery(2) temp.wLotterySellGazeOnPrice(1)-temp.wLotterySellGazeOnPrice(2) temp.wLotterySellGazeOnLottery(1)-temp.wLotterySellGazeOnLottery(2)],...
                            [temp.wLotteryBuyGazeOnPrice(3)-temp.wLotteryBuyGazeOnPrice(1) temp.wLotteryBuyGazeOnLottery(3)-temp.wLotteryBuyGazeOnLottery(1) temp.wLotterySellGazeOnPrice(3)-temp.wLotterySellGazeOnPrice(1) temp.wLotterySellGazeOnLottery(3)-temp.wLotterySellGazeOnLottery(1)],...
                            'Color',[0 0 0]);
er.LineStyle = 'none';                           
% Edit figure
set(gca,'TickDir','out');
xticklabels({'BuyerP','BuyerL','SellerP','SellerL'});
ylabel('wLottery');
% yticks(1:0.5:2.5);
ylim([0 1]);
hold off
print(1, '-dtiff', [root_path,'\figure\model\addm\wLottery_addm6.tif'], '-r200');
close;

%% Plot: valuation bias raw
figure('Renderer', 'painters', 'Position', [10 10 380 350]);
ax=subplot(1,1,1);
temp=addm.group.post.describe;
ab=bar([1 2 3 4],[temp.raw_ValBiasSellOnPriceBuyOnLotteryLog(1) temp.raw_ValBiasSellOnPriceBuyOnPriceLog(1) temp.raw_ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.raw_ValBiasSellOnLotteryBuyOnPriceLog(1)],'EdgeColor','none','FaceColor',bias_color_median{1},'barwidth',0.6);
ab.FaceColor='flat';
% ab.CData(1,:) = attribute_color{1};
% ab.CData(2,:) = attribute_color{2};
% ab.CData(3,:) = attribute_color{1};
% ab.CData(4,:) = attribute_color{2};
hold on
er=errorbar([1 2 3 4],[temp.raw_ValBiasSellOnPriceBuyOnLotteryLog(1) temp.raw_ValBiasSellOnPriceBuyOnPriceLog(1) temp.raw_ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.raw_ValBiasSellOnLotteryBuyOnPriceLog(1)],...
                            [temp.raw_ValBiasSellOnPriceBuyOnLotteryLog(1)-temp.raw_ValBiasSellOnPriceBuyOnLotteryLog(2) temp.raw_ValBiasSellOnPriceBuyOnPriceLog(1)-temp.raw_ValBiasSellOnPriceBuyOnPriceLog(2) temp.raw_ValBiasSellOnLotteryBuyOnLotteryLog(1)-temp.raw_ValBiasSellOnLotteryBuyOnLotteryLog(2) temp.raw_ValBiasSellOnLotteryBuyOnPriceLog(1)-temp.raw_ValBiasSellOnLotteryBuyOnPriceLog(2)],...
                            [temp.raw_ValBiasSellOnPriceBuyOnLotteryLog(3)-temp.raw_ValBiasSellOnPriceBuyOnLotteryLog(1) temp.raw_ValBiasSellOnPriceBuyOnPriceLog(3)-temp.raw_ValBiasSellOnPriceBuyOnPriceLog(1) temp.raw_ValBiasSellOnLotteryBuyOnLotteryLog(3)-temp.raw_ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.raw_ValBiasSellOnLotteryBuyOnPriceLog(3)-temp.raw_ValBiasSellOnLotteryBuyOnPriceLog(1)],...
                            'Color',[0 0 0]);
er.LineStyle = 'none';                           
% Edit figure
set(gca,'TickDir','out');
% xticklabels({'BuyerOnPrice','BuyerOnLottery','SellerOnPrice','SellerOnLottery'});
ylabel('Valuation Bias');
% yticks(1:0.5:2.5);
ylim([-1 1.5]);
% yticks(0:1:3);
% line(xlim,[1 1],'Color',[0 0 0],'LineStyle','--');
hold off
print(1, '-dtiff', [root_path,'\figure\model\addm\val_addm6_raw.tif'], '-r200');
close; 
      
%% Plot: valuation bias modulated by theta
figure('Renderer', 'painters', 'Position', [10 10 380 350]);
ax=subplot(1,1,1);
temp=addm.group.post.describe;
ab=bar([1 2 3 4],[temp.ValBiasSellOnPriceBuyOnLotteryLog(1) temp.ValBiasSellOnPriceBuyOnPriceLog(1) temp.ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.ValBiasSellOnLotteryBuyOnPriceLog(1)],'EdgeColor','none','FaceColor',bias_color_median{1},'barwidth',0.6);
ab.FaceColor='flat';
% ab.CData(1,:) = attribute_color{1};
% ab.CData(2,:) = attribute_color{2};
% ab.CData(3,:) = attribute_color{1};
% ab.CData(4,:) = attribute_color{2};
hold on
er=errorbar([1 2 3 4],[temp.ValBiasSellOnPriceBuyOnLotteryLog(1) temp.ValBiasSellOnPriceBuyOnPriceLog(1) temp.ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.ValBiasSellOnLotteryBuyOnPriceLog(1)],...
                            [temp.ValBiasSellOnPriceBuyOnLotteryLog(1)-temp.ValBiasSellOnPriceBuyOnLotteryLog(2) temp.ValBiasSellOnPriceBuyOnPriceLog(1)-temp.ValBiasSellOnPriceBuyOnPriceLog(2) temp.ValBiasSellOnLotteryBuyOnLotteryLog(1)-temp.ValBiasSellOnLotteryBuyOnLotteryLog(2) temp.ValBiasSellOnLotteryBuyOnPriceLog(1)-temp.ValBiasSellOnLotteryBuyOnPriceLog(2)],...
                            [temp.ValBiasSellOnPriceBuyOnLotteryLog(3)-temp.ValBiasSellOnPriceBuyOnLotteryLog(1) temp.ValBiasSellOnPriceBuyOnPriceLog(3)-temp.ValBiasSellOnPriceBuyOnPriceLog(1) temp.ValBiasSellOnLotteryBuyOnLotteryLog(3)-temp.ValBiasSellOnLotteryBuyOnLotteryLog(1) temp.ValBiasSellOnLotteryBuyOnPriceLog(3)-temp.ValBiasSellOnLotteryBuyOnPriceLog(1)],...
                            'Color',[0 0 0]);
er.LineStyle = 'none';                           
% Edit figure
set(gca,'TickDir','out');
% xticklabels({'BuyerOnPrice','BuyerOnLottery','SellerOnPrice','SellerOnLottery'});
ylabel('Valuation Bias');
% yticks(1:0.5:2.5);
ylim([-1 1.5]);
% yticks(0:1:3);
% line(xlim,[1 1],'Color',[0 0 0],'LineStyle','--');
hold off
print(1, '-dtiff', [root_path,'\figure\model\addm\val_addm6_log.tif'], '-r200');
close;

%%
toc










%%
dic_raw=readtable([root_path,'\model\hddm\aDDM4a\aDDM4a_dic.csv']);
dic.aDDM4a=dic_raw.dic(1);
dic_raw=readtable([root_path,'\model\hddm\aDDM2a\aDDM2a_dic.csv']);
dic.aDDM2a=dic_raw.dic(1);

%%
